﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BISPOS
{
    public partial class POSChangePriceForm : DevExpress.XtraEditors.XtraForm
    {
        private POSForm posForm;
        public POSChangePriceForm(POSForm _posForm)
        {
            InitializeComponent();
            posForm = _posForm;

            productTextEdit.Text = posForm.selectedItem.ProductName;
            oldPriceTextEdit.Text = posForm.selectedItem.PriceAmount.ToString("N2");
            oldTotalPriceTextEdit.Text = posForm.selectedItem.TotalPrice.ToString("N2");


            newPriceTextEdit.Focus();
        }

        private void newPriceTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                newTotalPriceTextEdit.Text = (Convert.ToDecimal(newPriceTextEdit.Text) * Convert.ToDecimal(posForm.selectedItem.Quantity)).ToString("N2");
            }
            catch { }
        }

        private void newPriceTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    BISPOS.Repository.ProductRepository repository = new Repository.ProductRepository();

                    Price price = repository.AddSpecialPrice(posForm.selectedItem.ProductId, Convert.ToDecimal(this.newPriceTextEdit.Text));
                    POSGridItem tempItem = posForm.selectedItem;
                    posForm.pOSGridItemBindingSource.Remove(posForm.selectedItem);
                    
                    tempItem.PriceId = price.Id;
                    tempItem.PriceAmount = price.Amount;

                    posForm.pOSGridItemBindingSource.Add(tempItem);
                    posForm.UpdateTotalPrice();
                    this.Close();
                }
                catch { MessageBox.Show("ราคาไม่ถูกต้อง");}
            }
        }
    }
}